-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang][test] Add test for comma operator rejection in preprocessor conditionals #155570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…expressions Add test coverage for comma operator usage in #if preprocessor directives to ensure it continues to be properly rejected across all C++ standard versions. Per CWG 1436, comma operators are not among the permitted operators in preprocessor conditional expressions. Addresses llvm#132822
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-clang Author: NohHyeon Kwon (swote-git) ChangesAdd test coverage to ensure comma operators remain properly rejected in Per CWG 1436, comma is not among the permitted operators in preprocessor conditional expressions. This test prevents regressions and clarifies the intended behavior. Addresses #132822 Full diff: https://github.com/llvm/llvm-project/pull/155570.diff 1 Files Affected:
diff --git a/clang/test/Preprocessor/cxx_oper_comma.cpp b/clang/test/Preprocessor/cxx_oper_comma.cpp
new file mode 100644
index 0000000000000..5589024ede01c
--- /dev/null
+++ b/clang/test/Preprocessor/cxx_oper_comma.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++98
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++11
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++14
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++17
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++20
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++23
+
+// Test 1: Top-level comma
+// expected-error@+1 {{expected end of line in preprocessor expression}}
+#if 1, 2
+#endif
+
+// Test 2: Comma in conditional expression
+// expected-error@+1 {{comma operator in operand of #if}}
+#if 1 ? 1, 0 : 3
+#endif
+
+// Test 3: Parenthesized comma
+// expected-error@+1 {{comma operator in operand of #if}}
+#if (1, 2)
+#endif
+
+// Test 4: Multiple commas
+// expected-error@+1 {{expected end of line in preprocessor expression}}
+#if 1, 2, 3
+#endif
|
|
Currently, this only about adding test code. While adding these tests, I noticed two potential improvements to the current error message
A clearer message should be more clear. |
Sirraide
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, though I wonder if it should be part of the DR tests instead.
CC @Endilll
|
@Sirraide Thanks for your review! Should I move this to clang/test/CXX/drs/cwg1436.cpp and format it according to the DR test conventions? |
Something like that yeah; I think there might already be a file that contains some of the 14XX tests somewhere but I’m not sure; I’ve tagged Vlad about this who should be able to tell you where exactly to put it. |
|
@shafik was https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#3017 resolved ?
Yes, please! |
|
There are some tests in |
|
@DavidSpickett Thanks for your help. I can see similar test in // PR2284 - The constant-expr production does not including comma.
// CHECK: [[@LINE+1]]:14: error: expected end of line in preprocessor expression
#if 1 ? 2 : 0, 1
#endifWhat I wanted to write a little more comprehensively was because of the contents of // Comma is not allowed in C89
// RUN: not %clang_cc1 -E %s -std=c89 -pedantic-errors
// Comma is allowed if unevaluated in C99
// RUN: %clang_cc1 -E %s -std=c99 -pedantic-errors
// PR2279
#if 0? 1,2:3
#endif |
|
I'd love if this PR added tests for Core issues 1436 and 3017, but I don't see which wording we should test against. The only proposed resolution from 2012-02 by Richard is obviously incomplete. I think we shouldn't touch DR tests until more information from Core arrives. |
|
@Endilll Thanks for pointing out CWG 3017. // Test 2: Comma in conditional expression
// expected-error@+1 {{comma operator in operand of #if}}
#if 1 ? 1, 0 : 3
#endifShould I add a comment referencing CWG 3017 to make this connection explicit? |
|
Sure, additional context never hurts. |
- Add test coverage for comma operator in #elif directive - Reference CWG 3017 in comments for additional context - Addresses reviewer feedback
|
@DavidSpickett @Endilll i made changes for adding context(cwg3017) and adding tests( Please review when you have time. |
|
Tests look fine to me now, but deferring to @Endilll as the domain expert here. |
Endilll
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your work
No, it is still active. It looks like CWG1436 is where the action will be moving forward. |
Remove defined(__has_embed) check and use since-cxx23 verify prefix to align with C++ DR test conventions.
Endilll
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is good to go after you add #if , test suggested earlier
Regarding the #if , test case, I believe I've already added it in the previous version of the patch(test 6). You can find it right here on line36-39. Thanks for your support! |
|
@swote-git Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…onditionals (llvm#155570) Add test coverage to ensure comma operators remain properly rejected in `#if` directives. Per CWG 1436, comma is not among the permitted operators in preprocessor conditional expressions. This test prevents regressions and clarifies the intended behavior. Fixes llvm#132822
…onditionals (llvm#155570) Add test coverage to ensure comma operators remain properly rejected in `#if` directives. Per CWG 1436, comma is not among the permitted operators in preprocessor conditional expressions. This test prevents regressions and clarifies the intended behavior. Fixes llvm#132822
…onditionals (llvm#155570) Add test coverage to ensure comma operators remain properly rejected in `#if` directives. Per CWG 1436, comma is not among the permitted operators in preprocessor conditional expressions. This test prevents regressions and clarifies the intended behavior. Fixes llvm#132822
Add test coverage to ensure comma operators remain properly rejected in
#ifdirectives.Per CWG 1436, comma is not among the permitted operators in preprocessor conditional expressions. This test prevents regressions and clarifies the intended behavior.
Fixes #132822